#include "include.h"
#include "func.h"
#include "func_music.h"
#include "func_exspiflash_demo.h"
#include "api_spiflash_ex.h"

#if 0//(EX_SPIFLASH_SUPPORT)
#define MAX_PRV_SONG 5
#define MAX_CUR_SONG 2
#define MAX_AFTER_SONG 5
#define MAX_DEMO_TIMEOUT_TIME 5

demo_msc_t demo_msc AT(.buf.func.demo);

AT(.text.func.demo.table)
const demo_play_time_info_t play_time_info[2]=
{
	{489,661,0x54f,0x2fbe0},//song 1 48.8-1.09.100
	{0,150,0x0,0x0},//song 2 0-17.6
};
AT(.text.func.demo.table)
const u8 fade_in_out_falg[10]=
{
	3,6,9,12,15,18,21,24,27,30
};
AT(.text.func.demo.table)
const u8 max_demo_song_table[]=
{
	1,2,2
};
AT(.text.func.demo.table)
const u8 demo_song_table[3][2]=
{
	{25,0},
	{6,7},
	{12,24},
};
AT(.text.func.demo.table)
const u8 demo_light_table[3][2]=
{
	{LIGHT_SEQUENCE_11,LIGHT_SEQUENCE_11},
	{LIGHT_SEQUENCE_5,LIGHT_SEQUENCE_5},
	{LIGHT_SEQUENCE_3A,LIGHT_SEQUENCE_3B},
};

AT(.text.func.demo.table)
const u16 demo_delay_time[3][2]=
{
	{500,500,},
	{500,0},
	{500,3000},
};

AT(.text.func.demo.table)
const u8 demo_sfx_song_table[]=
{
	16,17,18,19,20,21,9,8
};

AT(.text.func.demo.table)
const u8 demo_sfx_light_mode_table[]=
{
	LIGHT_SEQUENCE_7,
	LIGHT_SEQUENCE_8,
	LIGHT_SEQUENCE_9,
	LIGHT_SEQUENCE_10,
	LIGHT_SEQUENCE_11,
	LIGHT_SEQUENCE_12,
	LIGHT_SEQUENCE_13,
	LIGHT_SEQUENCE_2,
};

AT(.text.func.demo.table)
const song_time_play_t demo_song1_time[]=
{
	{0,LIGHT_SEQUENCE_7},		//1
	{797,LIGHT_SEQUENCE_8},
	{1622,LIGHT_SEQUENCE_4B},
	{2212,LIGHT_SEQUENCE_4C},
	{2435,LIGHT_SEQUENCE_4A},
	{3036,LIGHT_SEQUENCE_4C},
	{3250,LIGHT_SEQUENCE_4B},
	{4065,LIGHT_SEQUENCE_4A},
	{4675,LIGHT_SEQUENCE_4C},
	{4904,LIGHT_SEQUENCE_4D},//10
	{6401,LIGHT_SEQUENCE_16A},
	{6578,LIGHT_SEQUENCE_16},
	{6654,LIGHT_SEQUENCE_4D},
	{6907,LIGHT_SEQUENCE_16},
	{6987,LIGHT_SEQUENCE_16A},
	{7066,LIGHT_SEQUENCE_4D},
	{8069,LIGHT_SEQUENCE_1},
	{8580,LIGHT_SEQUENCE_4A},
	{9131,LIGHT_SEQUENCE_4C},
	{9345,LIGHT_SEQUENCE_4B},//20
	{9947,LIGHT_SEQUENCE_4C},
	{10165,LIGHT_SEQUENCE_4A},
	{10977,LIGHT_SEQUENCE_4B},
	{11611,LIGHT_SEQUENCE_1},
	{11870,LIGHT_SEQUENCE_4D},
	{13358,LIGHT_SEQUENCE_16A},
	{13489,LIGHT_SEQUENCE_16},
	{13573,LIGHT_SEQUENCE_16A},
	{13645,LIGHT_SEQUENCE_4D},
	{13830,LIGHT_SEQUENCE_16},//30
	{13902,LIGHT_SEQUENCE_16A},
	{13977,LIGHT_SEQUENCE_17},
	{14110,LIGHT_SEQUENCE_4D},
	{15104,LIGHT_SEQUENCE_4A},
	{15253,LIGHT_SEQUENCE_4B},
	{15530,LIGHT_SEQUENCE_4A},
	{15683,LIGHT_SEQUENCE_4D},
	{15855,LIGHT_SEQUENCE_4A},
	{16082,LIGHT_SEQUENCE_4B},//40
	{16340,LIGHT_SEQUENCE_4B},
	{16488,LIGHT_SEQUENCE_4D},
	{16665,LIGHT_SEQUENCE_4B},
	{17491,LIGHT_SEQUENCE_4A},
	{18066,LIGHT_SEQUENCE_1},
	{18519,LIGHT_SEQUENCE_4D},
	{20131,LIGHT_SEQUENCE_4A},
	{20213,LIGHT_SEQUENCE_4B},
	{20282,LIGHT_SEQUENCE_4D},
	{20535,LIGHT_SEQUENCE_4B},	//50
	{20613,LIGHT_SEQUENCE_4A},
	{20695,LIGHT_SEQUENCE_17},
	{20946,LIGHT_SEQUENCE_4D},
	{21692,LIGHT_SEQUENCE_1},
	{22118,LIGHT_SEQUENCE_17},
	{22202,LIGHT_SEQUENCE_18}, //56
};
AT(.text.func.demo.table)
const song_time_play_t demo_song2_time[]=
{
	{0,LIGHT_SEQUENCE_1},		//1
	{149,LIGHT_SEQUENCE_10},
	{1913,LIGHT_SEQUENCE_4A},
	{2339,LIGHT_SEQUENCE_1},
	{2582,LIGHT_SEQUENCE_17},
	{2685,LIGHT_SEQUENCE_4A},
	{3425,LIGHT_SEQUENCE_17},
	{3516,LIGHT_SEQUENCE_4A},
	{4654,LIGHT_SEQUENCE_1},
	{5030,LIGHT_SEQUENCE_10},//10
	{6452,LIGHT_SEQUENCE_17},
	{6527,LIGHT_SEQUENCE_13},
	{8073,LIGHT_SEQUENCE_17},
};


AT(.text.func.demo)
bool exspiflash_demo_init(void)
{
    printf("exspiflash_init\n");
    spiflash1_init();
    if (!is_exspiflash_online()) {
        return false;
    }
    return true;
}
AT(.text.func.demo)
void set_demo_play_num(void)
{
	u8 song_index=demo_msc.song_info[demo_msc.song_mode].song_index;
	u8 set_light_mode;
	demo_msc.pause=false;
	demo_msc.play_time=0;
	demo_msc.song_ticks=0;
	amp_speaker_unmute();
	bsp_set_volume(MUSIC_VOL);
	if(demo_msc.sfx_play_flag)
	{
		demo_msc.cur_num=demo_sfx_song_table[sys_cb.sfx_index-1];
		set_light_mode=demo_sfx_light_mode_table[sys_cb.sfx_index-1];
		printf("sfx cur_num:%d\n",demo_msc.cur_num);
	}
	else
	{
		demo_msc.cur_num= demo_song_table[demo_msc.song_mode][song_index];
		set_light_mode=demo_light_table[demo_msc.song_mode][song_index];
		if(demo_msc.song_mode==1)
		{		
			demo_msc.song_play_flag=1;
			if(song_index==0)
			{
				msc_breakpiont_t song_brkpt;//用于保存和恢复播放位置的结构体			
				song_brkpt.frame_count=play_time_info[song_index].frame_count;
				song_brkpt.file_ptr=play_time_info[song_index].file_ptr;
				music_set_cur_time(play_time_info[song_index].start_time);							
				exspifalsh_music_num_kick(demo_msc.cur_num);
				music_set_jump(&song_brkpt);
				delay_ms(50);
				demo_msc.time_offset=9;
				set_light_mode=demo_song1_time[demo_msc.time_offset].light_mode;
			}
			else
			{
				exspifalsh_music_num_kick(demo_msc.cur_num);
				demo_msc.time_offset=0;
				set_light_mode=demo_song2_time[demo_msc.time_offset].light_mode;
			}
			bsp_set_light_mode(set_light_mode);			
			return;
		}
	}	
	
	exspifalsh_music_num_kick(demo_msc.cur_num);
	#if 1
	if(demo_msc.sfx_play_flag)
	{
		bsp_set_sfx_mode(set_light_mode);
	}
	else
	{
		bsp_set_light_mode(set_light_mode);
	}
	#endif
}
AT(.text.func.demo)
void func_check_demo_play_status(void)
{
	if (get_music_dec_sta() == MUSIC_STOP)
	{
		u16 delay_time;
		if(demo_msc.pause)
		{
			return;
		}
		if(demo_msc.sfx_play_flag)
		{
			demo_msc.pause=true;
			demo_msc.timeout_cnt=0;
			demo_msc.sfx_play_flag=false;
			return;
		}
		if(demo_msc.song_mode==3)
		{
			demo_msc.pause=true;
			demo_msc.timeout_cnt=0;
			demo_msc.sfx_play_flag=false;
			bsp_set_light_mode(LIGHT_SEQUENCE_11);
			return;
		}
		u8 song_index=demo_msc.song_info[demo_msc.song_mode].song_index;
		demo_msc.song_play_flag=0;
		delay_time=demo_delay_time[demo_msc.song_mode][song_index];
		if(delay_time!=0)
		{

			if(tick_check_expire(demo_msc.ticks_1,delay_time))
			{
				demo_msc.ticks_1=tick_get();
				if(!demo_msc.tick_start)
				{
					printf("tick start:%d\n",delay_time);
					if(delay_time==3000)
					{
						bsp_set_light_mode(LIGHT_SEQUENCE_11);
					}
					demo_msc.tick_start=1;
					return;
				}
			}
			else
			{
				return;
			}
			demo_msc.tick_start=0;
			printf("tick end!!\n");
		}
		if(demo_msc.song_mode==0)
		{
			demo_msc.song_mode=1;
			demo_msc.song_info[1].song_index=0;
		}
		else if(demo_msc.song_mode==1)
		{
			#if FUNC_NO_SPEECH
			demo_msc.song_mode=3;
			return;
			#else
			demo_msc.song_mode=2;
			demo_msc.song_info[2].song_index=0;
			#endif
		}
		else if(demo_msc.song_mode==2)
		{
			demo_msc.song_info[2].song_index++;
			if(demo_msc.song_info[2].song_index>=max_demo_song_table[2])
			{
				demo_msc.song_info[2].song_index=0;
				demo_msc.song_mode=3;
				return;
			}
		}
		demo_msc.song_play_flag=1;
		set_demo_play_num();

	}
}


AT(.text.func.demo)
void func_demo_light_sync_deal(void)
{

	u16 song_play_time=0;
	u8 light_mode=0;
	if(demo_msc.pause)
	{
		return;
	}
	if(demo_msc.cur_num!=6&&demo_msc.cur_num!=7)
	{
		return;
	}
	#if 1
	if(tick_check_expire(demo_msc.song_ticks,10))
	{
		demo_msc.song_ticks = tick_get();
		//printf("cur_num:%d\n",demo_msc.cur_num);
		if(demo_msc.cur_num==6)
		{
			song_play_time=demo_song1_time[demo_msc.time_offset].time;
			light_mode=demo_song1_time[demo_msc.time_offset].light_mode;
		}
		else if(demo_msc.cur_num==7)
		{
			song_play_time=demo_song2_time[demo_msc.time_offset].time;
			light_mode=demo_song2_time[demo_msc.time_offset].light_mode;
		}
		if(demo_msc.play_time==song_play_time)
		{

			u32 cur_time1 = music_get_cur_time();
			printf("time_offset:%d\n",demo_msc.time_offset);
		    printf("cur_time:%d->song_time_offset:%d\n",cur_time1,demo_msc.play_time);
			if(demo_msc.play_time!=(cur_time1/10))
			{
				demo_msc.play_time=cur_time1*10;
			}
			bsp_set_light_mode(light_mode);
			demo_msc.time_offset++;
		}
		demo_msc.play_time++;
	}
	#endif
}
AT(.text.func.demo)
void func_demo_lower_power_check(void)
{
	#if SYS_KARAOK_EN
	if(sys_cb.mic_onoff&&sys_cb.karaok_init)
	 {
	 	u8 mic_off;
	 	mic_off=bsp_get_mic_vol_offset();
		if(sys_cb.mic_vol_offset!=mic_off)
		{
			sys_cb.mic_vol_offset=mic_off;
			bsp_karaok_set_mic_volume();
		}
		if ((sys_cb.vbat <=2700) ||(adc_cb.vrtc_val>=0x39f))
		 {
		 	sys_cb.vbat_low=1;
			bsp_karaok_exit(AUDIO_PATH_KARAOK);
		 }			 	
	 }
	#endif
}

AT(.text.func.demo)
void func_exspifalsh_demo_process(void)
{
    func_process();
	func_check_demo_play_status();
	func_demo_light_sync_deal();
}

AT(.text.func.demo)
void func_demo_switch_song(void)
{
	music_control(MUSIC_MSG_STOP);
	switch(demo_msc.song_mode)
	{
		case 0:
			demo_msc.song_mode=1;
			demo_msc.song_info[demo_msc.song_mode].song_index=0;
			demo_msc.song_play_flag=1;
		break;
		case 1:
			demo_msc.song_play_flag=1;
			demo_msc.vol_adjust=0;
			demo_msc.song_info[1].song_index++;
			if(demo_msc.song_info[1].song_index>=max_demo_song_table[1])
			{
				demo_msc.song_info[1].song_index=0;
			}
		break;
		case 2:
		case 3:
			demo_msc.song_mode=1;
			demo_msc.song_play_flag=1;
			demo_msc.song_info[1].song_index++;
		    if(demo_msc.song_info[1].song_index>=max_demo_song_table[1])
			{
				demo_msc.song_info[1].song_index=0;
			}
		break;
		
		default:
		break;
	}
	printf("song_mode:%d\n",demo_msc.song_mode);
	demo_msc.ticks_1=0;
	demo_msc.sfx_play_flag=0;
	demo_msc.need_adjust=0;
	bsp_change_volume(MUSIC_VOL);
	set_demo_play_num();
}
AT(.text.func.demo)
void exspifalsh_demo_switch_file(u8 direction)   //1 next , 0 prev
{
    music_control(MUSIC_MSG_STOP);
    printf("direction = %d, music_num = %d, total = %d\n",direction, demo_msc.cur_num,demo_msc.total_num);
	func_demo_switch_song();

}


AT(.text.func.demo)
void func_demo_song_info_init(void)
{
	u8 i;
	for(i=0;i<3;i++)
	{
		demo_msc.song_info[i].song_index=0;
	}
}
AT(.text.func.demo)
void func_demo_check_timeout(void)
{
	if(demo_msc.pause)
	{
		demo_msc.timeout_cnt++;
		amp_speaker_mute();
		printf("timeout cnt:%d\n",demo_msc.timeout_cnt);
		if(demo_msc.timeout_cnt>=demo_msc.max_timeout_cnt)
		{
			demo_msc.timeout_cnt=0;
			demo_msc.song_int_flag=0;
			sys_cb.pwrdwn_tone_en=2;
			 music_control(MUSIC_MSG_STOP);
			func_cb.sta = FUNC_PWROFF;
		}
	}
}
AT(.text.func.demo)
void func_demo_sfx_ctl(void)
{
	printf("sfx_index:%d\n",sys_cb.sfx_index);
	sys_cb.sfx_index++;
	if(sys_cb.sfx_index>8)
	{
		sys_cb.sfx_index=1;

	}
	#if 1
	music_control(MUSIC_MSG_STOP);
	demo_msc.sfx_play_flag=true;
	set_demo_play_num();
	#else
	if(demo_msc.pause)
	{
		demo_msc.sfx_play_flag=true;
		set_demo_play_num();
	}
	else
	{
		if(sys_cb.sfx_index==0)
		{
			bsp_set_sfx_mode(LIGHT_SEQUENCE_11);
		}
		else
		{
			if(demo_msc.sfx_play_flag||demo_msc.song_mode!=1)
			{
				music_control(MUSIC_MSG_STOP);
				demo_msc.sfx_play_flag=true;
				set_demo_play_num();
			}
			else
			{
				bsp_set_sfx_mode(demo_sfx_light_mode_table[sys_cb.sfx_index-1]);
			}

		}

	}
	#endif

}

AT(.text.func.demo)
bool exspifalsh_demo_image_check(void)
{
    if (is_exspiflash_online()) {
        demo_msc.total_num = exspifalsh_music_get_file_total();
		printf("exspi_msc.total_num = %d\n",demo_msc.total_num);
		#if 0
		if(!exspi_msc.project_num&&exspi_msc.total_num>1)
		{
			exspi_msc.total_num=exspi_msc.total_num/2;
			printf("project total num= %d\n",exspi_msc.total_num);
		}
        #endif
        if (demo_msc.total_num > 0) {
            return true;
        }
    }
    return false;
}

AT(.text.func.demo)
static void func_exspifalsh_demo_enter(void)
{
    msg_queue_clear();
	func_demo_song_info_init();
	exspiflash_demo_init();
	if ((!exspifalsh_demo_image_check())){
        func_cb.sta = FUNC_BT;
        return;
    }
	func_cb.mp3_res_play = NULL;//func_exspifalsh_mp3_res_play;
    register_spi_read_function(spiflash1_read);
	
	#if SYS_KARAOK_EN
	bsp_karaok_exit(AUDIO_PATH_KARAOK);
	#endif
	bsp_set_volume(MUSIC_VOL);
	#if FUNC_NO_SPEECH
	demo_msc.song_mode=1;
	#else
	demo_msc.song_mode=0;
	#endif
	demo_msc.timeout_cnt=0;
	demo_msc.max_timeout_cnt=MAX_DEMO_TIMEOUT_TIME;
	set_demo_play_num();
}

AT(.text.func.demo)
static void func_exspifalsh_demo_exit(void)
{
    music_control(MUSIC_MSG_STOP);
	sys_cb.exspiflash_music_mode=0;
	if(sys_cb.pwrdwn_tone_en!=2)
	{
		sys_cb.pwrdwn_tone_en=1;
	}	
    register_spi_read_function(NULL);  //恢复内部SPI读接口.
    func_cb.last = FUNC_MUSIC;
}
#if 1

AT(.text.func.music)
void auto_stop_play_timer(void)
{
	static u32 ticks = 0;

	u16 check_time;
	u8 need_stop=false;
	if(sys_cb.cur_mode!=DEMO_MODE)
	{
		return;
	}
	if(demo_msc.pause)
	{
		return;
	}
	if(demo_msc.cur_num!=6&&demo_msc.cur_num!=7)
	{
		return;
	}
	if(!demo_msc.song_play_flag)
	{
		return;
	}
    if(tick_check_expire(ticks,100)){      //100ms记忆一次
    	//msc_breakpiont_t brkpt;
        ticks = tick_get();
        check_time = music_get_cur_time();
		//printf("check_time:%d\n",check_time);
		//music_get_breakpiont(&brkpt);
		//printf("time:%d->frame_count:0x%x->file_ptr:0x%x->fname_crc:0x%x\n",check_time,brkpt.frame_count,brkpt.file_ptr,brkpt.fname_crc);
		#if 1
		if(demo_msc.need_adjust)
		{
			printf("play time:0x%x->adjust_cnt:%d\n",check_time,demo_msc.adjust_cnt);
			if(demo_msc.vol_adjust==1)
			{
				demo_msc.adjust_cnt++;
				if(demo_msc.adjust_cnt==fade_in_out_falg[sys_cb.vol])
				{
					bsp_set_volume(bsp_volume_inc(sys_cb.vol));
				}
				if(demo_msc.adjust_cnt==21||sys_cb.vol==VOL_MAX)
				{
					demo_msc.need_adjust=0;
				}
			}
			else
			{
				demo_msc.adjust_cnt++;
				if(demo_msc.adjust_cnt==fade_in_out_falg[VOL_MAX-sys_cb.vol])
				{
					bsp_set_volume(bsp_volume_dec(sys_cb.vol));
				}
				if(demo_msc.adjust_cnt==30||sys_cb.vol==0)
				{
					demo_msc.need_adjust=0;
					need_stop=true;
				}
			}
		}

		if((demo_msc.need_adjust==0)&&(check_time==play_time_info[demo_msc.song_info[demo_msc.song_mode].song_index].end_time||
			(check_time==play_time_info[demo_msc.song_info[demo_msc.song_mode].song_index].end_time-1)))
		{
			demo_msc.need_adjust=1;
			demo_msc.vol_adjust=0;
			demo_msc.adjust_cnt=0;
			printf("sys_cb.vol:%d\n",sys_cb.vol);
		}
		if(need_stop)
		{
			music_control(MUSIC_MSG_STOP);
			bsp_set_volume(MUSIC_VOL);
		}
		#endif

    }
}



AT(.text.func.music)
void non_volatile_memory(void)
{
    #if 0//SPIFALSH_MUSIC_BREAK_MEMORY
    static u32 ticks = 0;
    u32 curFileLen = 0;
    u32 ptrTemp = 0;
    if(tick_check_expire(ticks,100)){      //100ms记忆一次
        ticks = tick_get();
        music_get_breakpiont(&(demo_msc.brkpt));

        curFileLen = exspiflash_music_get_cur_file_len();
        if(demo_msc.brkpt.file_ptr >= curFileLen)
        {
            ptrTemp = (demo_msc.brkpt.file_ptr % curFileLen);
            demo_msc.brkpt.file_ptr = ptrTemp;
        }

        demo_msc.save_time = music_get_cur_time();
        demo_msc.save_num = demo_msc.cur_num;
        param_spiflash_breakpoint_write();
        param_sync();
    }
    #endif // SPIFALSH_MUSIC_BREAK_MEMORY
}
#endif
AT(.text.func.idle)
u8 demo_check_power_key(void)
{
	u8 key_cnt=0;
	u16 msg;
	while (1)
	{
	    WDT_CLR();
	    delay_ms(20);
		msg = msg_dequeue();
		printf("msg:0x%x\n",msg);
		if(msg==KL_POWER||msg==KH_POWER)
		{
			key_cnt=10;
			break;
		}
		key_cnt++;
		if(key_cnt>6)
		{
			break;
		}
	}
	if(key_cnt>=10)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

AT(.text.func.music)
void func_exspifalsh_demo(void)
{
    printf("%s\n", __func__);
	if(demo_check_power_key()==0)
	{
		printf("key long!!\n");
		demo_msc.longkey_wakeup=1;
	}
    func_exspifalsh_demo_enter();
    while (func_cb.sta == FUNC_EXSPIFLASH_DEMO) {
        func_exspifalsh_demo_process();
        func_exspifalsh_demo_message(msg_dequeue());
		auto_stop_play_timer();
    }
    func_exspifalsh_demo_exit();
}
#endif

